<?php

namespace app\api\controller;

use addons\zpwxsys\service\Token;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 工作推荐群
     */
    public function workCrowd(){
        $ret = \app\common\model\Crowd::find();
        if ($ret) {
            $this->success(__('成功'),$ret);
        } else {
            $this->error(__('失败'));
        }
    }
    /**
     * 首页展示区域
     */
    public function showArea(){

        //使用经纬度计算最近区县
        $userLng = input('lng');
        $userLat = input('lat');
        $earthRadius = 6371; // 地球半径(km)
        $maxDistance = 30; // 过滤30公里内的区县
        $nearest = Db::name('zpwxsys_area')->field([
            'id',
            'name',
            'lat',
            'lng',
            // Haversine公式计算距离
            "{$earthRadius} * acos(cos(radians({$userLat})) * cos(radians(lat)) * 
                 cos(radians(lng) - radians({$userLng})) + 
                 sin(radians({$userLat})) * sin(radians(lat)))" => 'distance'
        ])->having('distance', '<=', $maxDistance)
            ->order('distance', 'asc')
            ->find();

        $cityid = input('cityid');
        $area = [];
        $ret = Db::name('zpwxsys_area')->where('cityid',$cityid)->where('is_home',1)->order('sort', 'desc')->select();
        $area[] = [
            'id'=>$nearest?$nearest['id']:0,
            'name'=>'附近',
            'image'=>'/assets/addons/zpwxsys/16.png',
        ];
        foreach ($ret as $v){
            $area[] = [
                'id'=>$v['id'],
                'name'=>$v['name'],
                'image'=>$v['image'],
            ];
        }
        $area[] = [
            'id'=>99999,
            'name'=>'更多',
            'image'=>'/assets/addons/zpwxsys/20.png',
        ];
        if ($ret) {
            $this->success(__('成功'),$area);
        } else {
            $this->error(__('失败'));
        }
    }
}
